import urllib
import ssl, json, time


def FastShow(id):
    host = 'https://ali-deliver.showapi.com'
    path = '/showapi_expInfo'
    method = 'GET'
    appcode = '你自己的AppCode'
    querys = 'com=zhongtong&nu=535962308717'
    bodys = {}
    url = host + path + '?' + querys

    request = urllib.request.Request(url)
    request.add_header('Authorization', 'APPCODE ' + appcode)
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    response = urllib.request.urlopen(request, context=ctx)
    content = response.read()
    if (content):
        print(content)


def CompanyQuery(id):
    host = "http://www.kuaidi100.com"
    path = "/autonumber/autoComNum"
    query = "resultv2=1&text=" + id
    url = host + path + "?" + query
    request = urllib.request.Request(url)
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    response = urllib.request.urlopen(request, context=ctx)
    content = response.read()
    result = json.loads(content.decode(encoding='utf-8'))
    comcode = result["comCode"]
    resp = []
    if comcode:
        return resp.append(comcode)
    else:
        autos = result["auto"]
        for auto in autos:
            code = auto["comCode"]
            resp.append(code)

    return resp


def FastQuery(id):
    host = 'http://www.kuaidi100.com'
    path = '/query'
    method = 'GET'
    if id:
        list = CompanyQuery(id)
        for fast in list:
            querys = 'type=' + fast + '&postid=' + id + '&temp=' +str(time.time())
            url = host + path + '?' + querys

            request = urllib.request.Request(url)
            response = urllib.request.urlopen(request)
            content = response.read()
            result = json.loads(content.decode(encoding='utf-8'))
            message = result["message"]
            if message == 'ok':
                return result
            else:
                return ""
    else:
        ""



# {"message": "ok", "nu": "479542124103", "ischeck": "1", "condition": "F00", "com": "zhongtong", "status": "200",
#  "state": "3", "data": [
#     {"time": "2018-02-26 12:56:36", "ftime": "2018-02-26 12:56:36", "context": "[北京市] [北京崇文]的派件已签收 感谢使用中通快递,期待再次为您服务!",
#      "location": "北京崇文"}, {"time": "2018-02-26 09:18:05", "ftime": "2018-02-26 09:18:05",
#                            "context": "[北京市] 快件已到达[北京崇文],业务员王志伟正在第1次派件 电话:13370117072 请保持电话畅通、耐心等待",
#                            "location": "北京崇文"},
#     {"time": "2018-02-25 16:05:35", "ftime": "2018-02-25 16:05:35", "context": "[北京市] 快件离开 [北京]已发往[北京崇文]",
#      "location": "北京"},
#     {"time": "2018-02-25 15:43:08", "ftime": "2018-02-25 15:43:08", "context": "[北京市] 快件到达 [北京]", "location": "北京"},
#     {"time": "2018-02-25 01:37:02", "ftime": "2018-02-25 01:37:02", "context": "[漯河市] 快件离开 [漯河中转]已发往[北京]",
#      "location": "漯河中转"},
#     {"time": "2018-02-25 01:35:20", "ftime": "2018-02-25 01:35:20", "context": "[漯河市] 快件到达 [漯河中转]", "location": "漯河中转"},
#     {"time": "2018-02-24 19:10:15", "ftime": "2018-02-24 19:10:15", "context": "[商丘市] 快件离开 [商丘]已发往[北京]",
#      "location": "商丘"},
#     {"time": "2018-02-24 09:13:19", "ftime": "2018-02-24 09:13:19", "context": "[商丘市] [商丘]的商丘市场部已收件 电话:15237059688",
#      "location": "商丘"}]}
#
# {"message":"快递公司参数异常：单号不存在或者已经过期","nu":"","ischeck":"0","condition":"","com":"","status":"201","state":"0","data":[]}

# http: // www.kuaidi100.com / autonumber / autoComNum?resultv2 = 1 & text = 479542124103
# {"comCode": "", "num": "479542124103",
#  "auto": [{"comCode": "zhongtong", "id": "", "noCount": 2569, "noPre": "479542", "startTime": ""},
#           {"comCode": "minghangkuaidi", "id": "", "noCount": 11, "noPre": "479542", "startTime": ""}]}

if __name__ == '__main__':
    re = FastQuery("479542124103")
    print(re)
    com = re['com']
    print(com)

    con = CompanyQuery("479542124103")
    print(con)

    # import datetime
    # hour = datetime.datetime.now().hour
    # print(hour)
